package com.hm.service.impl.auth;

import com.hm.service.framework.base.BaseServiceImpl;
import com.hm.service.service.main.BaseHandleService;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class BaseHandleServiceImpl extends BaseServiceImpl implements BaseHandleService {

    private static final Logger logger = LoggerFactory.getLogger(BaseHandleServiceImpl.class);

    @Override
    public HashMap<String, String> getRequestInfo(HttpServletRequest req) {
        HashMap<String, String> resMap = new HashMap<>();
        try {
            String sessionId = req.getHeader("sessionId");
            String requestBody = req.getReader().readLine();
            //解析url
            String requestUrl = req.getRequestURL().toString();
            String[] arr = requestUrl.split("/");
            int index = arr.length - 1;//获取最后一个元素
            String serveName = arr[index];
            //封装返回信息
            resMap.put("sessionId", sessionId);
            resMap.put("requestBody", requestBody);
            resMap.put("serveName", serveName);
            return resMap;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String decryptRequestBody(String requestBody) {
        if (requestBody == null || requestBody.isEmpty()) {
            return generateFail("请求体为空");
        }
        return BaseDecrypt(requestBody);
    }

    @Override
    public HashMap<String, String> verifierServiceName(String serveName) {
        HashMap<String, String> resMap = new HashMap<>();
        if (serveName == null || serveName.isEmpty()) {
            resMap.put("state", "001");
            resMap.put("msg", "serveName异常");
        }
        resMap.put("state", "000");
        resMap.put("msg", "");
        return resMap;
    }

}
